package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.domain.act.activity.model.vo.CommonActSupportVO;
import org.fuys.coder.domain.act.activity.model.vo.CommonActVO;
import org.fuys.coder.domain.act.activity.repository.ICommonActRepository;
import org.fuys.coder.common.entity.PageDTO;
import org.fuys.coder.infrastructure.dao.CommonActMapper;
import org.fuys.coder.infrastructure.model.po.CommonAct;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: CommonActRepository
 * @author: WangJing
 * @description: 普通活动仓储服务实现类
 * @date: 2024/8/25 22:38
 * @version: 1.0
 */
@Service
public class CommonActRepository extends ServiceImpl<CommonActMapper, CommonAct>
        implements ICommonActRepository {
    @Resource
    private CommonActMapper commonActMapper;

    @Override
    public CommonActVO getById(Long id,Integer state) {
        final CommonAct commonAct = commonActMapper.selectOne(new LambdaQueryWrapper<CommonAct>()
                .select(CommonAct::getTitle, CommonAct::getCover, CommonAct::getDescription,
                        CommonAct::getBeginTime,CommonAct::getEndTime)
                .eq(CommonAct::getId, id).eq(CommonAct::getState,state));
        if(commonAct!=null){
            CommonActVO vo = new CommonActVO();
            vo.setActId(id);
            vo.setTitle(commonAct.getTitle());
            vo.setDescription(commonAct.getDescription());
            vo.setBeginTime(commonAct.getBeginTime());
            vo.setCoverId(commonAct.getCover());
            vo.setEndTime(commonAct.getEndTime());
            return vo;
        }
        return null;
    }

    @Override
    public CommonActSupportVO getSupportById(Long actId) {
        final CommonAct commonAct = commonActMapper.selectOne(new LambdaQueryWrapper<CommonAct>().select(CommonAct::getContent)
                .eq(CommonAct::getId, actId));
        if(commonAct==null){
            return null;
        }
        CommonActSupportVO vo = new CommonActSupportVO();
        vo.setContent(commonAct.getContent());
        return vo;
    }

    @Override
    public List<CommonActVO> getCurAct(int num) {
        Page<CommonAct> page=new Page<>();
        page.setCurrent(1);
        page.setSize(num);
        final Page<CommonAct> curAct = commonActMapper.selectPage(page, new LambdaQueryWrapper<CommonAct>()
                .select(CommonAct::getId, CommonAct::getCover, CommonAct::getTitle, CommonAct::getDescription,
                        CommonAct::getBeginTime, CommonAct::getEndTime)
                .eq(CommonAct::getState,1)
                .orderByDesc(CommonAct::getBeginTime));
        final List<CommonAct> records = curAct.getRecords();
        if(ObjectUtils.isEmpty(records)||ObjectUtils.isEmpty(records)){
            return null;
        }
        return records.stream().map(item->{
            CommonActVO vo = new CommonActVO();
            vo.setCoverId(item.getCover());
            vo.setActId(item.getId());
            vo.setTitle(item.getTitle());
            vo.setDescription(item.getDescription());
            vo.setBeginTime(item.getBeginTime());
            vo.setEndTime(item.getEndTime());
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Pair<List<CommonActVO>,Long> getPage(PageDTO pageDTO) {
        final Page<CommonAct> page = new Page<>();
        page.setCurrent(pageDTO.getCurPage());
        page.setSize(pageDTO.getPageNum());
        final Page<CommonAct> pageResult = commonActMapper.selectPage(page, new LambdaQueryWrapper<CommonAct>()
                .select(CommonAct::getId, CommonAct::getCover, CommonAct::getTitle, CommonAct::getDescription)
                .eq(CommonAct::getState, 1)
                .orderByDesc(CommonAct::getBeginTime));
        if(pageResult==null||pageResult.getRecords()==null){
            return null;
        }
        final List<CommonActVO> collect = pageResult.getRecords().stream()
                .map(item -> {
                    CommonActVO vo = new CommonActVO();
                    vo.setCoverId(item.getCover());
                    vo.setActId(item.getId());
                    vo.setTitle(item.getTitle());
                    vo.setDescription(item.getDescription());
                    vo.setBeginTime(item.getBeginTime());
                    vo.setEndTime(item.getEndTime());
                    return vo;
                }).collect(Collectors.toList());
        return new Pair<>(collect,pageResult.getTotal());
    }
}
